package com.shuyuansystem.service.impl;

import com.shuyuansystem.dao.OrderListMapper;
import com.shuyuansystem.pojo.OrderList;
import com.shuyuansystem.service.OrderListService;
import com.shuyuansystem.utils.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OrderListServiceImpl implements OrderListService {

    @Autowired
    private OrderListMapper orderListMapper;

    @Override
    public PageResult<OrderList> getOrdersByUserId(String userId, int pageNum, int pageSize) {
        int offset = (pageNum - 1) * pageSize;
        List<OrderList> orders = orderListMapper.findOrdersByUserId(userId, offset, pageSize);
        Long total = orderListMapper.getTotalCountByUserId(userId);
        return new PageResult<>(total, orders);
    }

    @Override
    public List<OrderList> getAllByMerchantOrderNumber(String MerchantOrderNumber) {
        return orderListMapper.getAllByMerchantOrderNumber(MerchantOrderNumber);
    }

    @Override
    public List<OrderList> getByMerchantOrderNumbers(List<String> merchantOrderNumbers) {
        return orderListMapper.getByMerchantOrderNumbers(merchantOrderNumbers);
    }

    @Override
    public boolean cancelOrderList(String merchantOrderNumber) {
        try {
            // 调用 OrderListMapper 中的方法删除订单信息
            int rowAffected = orderListMapper.deleteByMerchantOrderNumber(merchantOrderNumber);
            return rowAffected > 0;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}